
package com.azunitech.apm.bundles.jquery;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
import org.osgi.service.prefs.PreferencesService;
import com.azunitech.apm.bundles.jquery.api.Echo;

public class Activator implements BundleActivator, Echo {  
    // ServiceTracker for PreferencesServices  
    private ServiceTracker<PreferencesService, PreferencesService> serviceTracker;  
    // BundleContext  
    private BundleContext bc;  
    // Registration of Echo service  
    private ServiceRegistration<?> registration;  

    // activation  
    public void start(BundleContext context) throws Exception {  
        bc = context;  
        // init and start ServiceTracker to track PreferencesService  
        serviceTracker = new ServiceTracker<PreferencesService,PreferencesService>(context, PreferencesService.class.getName(), new Customizer());  
        serviceTracker.open();  
    }  

    // deactivation  
    public void stop(BundleContext context) throws Exception {  
        // stop ServiceTracker to track PreferencesService  
        serviceTracker.close();  
        serviceTracker = null;  
    }  

    public String echo(String str) {  
        return str;  
    }  

    /*
     * As Echo Service is depened on the preference services, so when the Preference Service is available
     * we register the Echo Service
     */
    // customizer that handles tracked service registration/modification/unregistration events  
    private class Customizer implements ServiceTrackerCustomizer<PreferencesService,PreferencesService> {  
    	
        public PreferencesService addingService(ServiceReference<PreferencesService> reference) {  
            // register Echo service  
            Dictionary<String, String> props = new Hashtable<String, String>();  
            props.put(ECHO_TYPE_PROP, "BundleActivator");  
            registration = bc.registerService(Echo.class.getName(), Activator.this, props);  
            return bc.getService(reference);
        }  

        public void removedService(ServiceReference<PreferencesService> reference, PreferencesService service) {  
            // unregister Echo service  
            registration.unregister();  
        }

		public void modifiedService(ServiceReference<PreferencesService> reference,	PreferencesService service) {
		}
    }  
}  
 